package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.customer.mapper.BankAccountMsgMapper;
import com.jzo2o.customer.mapper.ServeProviderMapper;
import com.jzo2o.customer.model.domain.BankAccountMsg;
import com.jzo2o.customer.model.domain.ServeProvider;
import com.jzo2o.customer.model.dto.request.BankAccountUpsertReqDTO;
import com.jzo2o.customer.model.dto.response.BankAccountResDTO;
import com.jzo2o.customer.service.IWorkerBankAccountMsgService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class IWorkerBankAccountMsgServiceImpl extends ServiceImpl<BankAccountMsgMapper, BankAccountMsg> implements IWorkerBankAccountMsgService {
    @Resource
    private ServeProviderMapper serveProviderMapper;
    @Resource
    private BankAccountMsgMapper bankAccountMsgMapper;
    @Override
    public BankAccountResDTO addOrUpdateBankMsg(BankAccountUpsertReqDTO dto) {
        if (dto.getName().length() <2 || dto.getName().length() >10){
            throw new ForbiddenOperationException("户名需要在2-10个字之间");
        }
        //网点在4-50个字之间
        if (dto.getBranch().length() <4 || dto.getBranch().length()>50){
            throw new ForbiddenOperationException("网点需要再4-50个字之间");
        }
        //银行账号在16.17.19位
        if (dto.getAccount().length() != 16 && dto.getAccount().length() != 17 && dto.getAccount().length() != 19){
            throw new ForbiddenOperationException("银行账号需要是16，17，19位");
        }
        Long userId = UserContext.currentUserId();
        //先根据id 判断是要新增或者修改
        BankAccountMsg bankAccountMsg = bankAccountMsgMapper.selectByServerProviderId(userId);
        //BankAccountMsg byId = getById(userId);
        //根据ID判断是企业还是个人
        ServeProvider serveProvider = serveProviderMapper.selectById(userId);
        Integer type = serveProvider.getType();
        if (bankAccountMsg != null){
            //修改
            //先把之前的信息删掉
            // bankAccountMsgMapper.deleteByUserId(userId);
            boolean update = lambdaUpdate().eq(BankAccountMsg::getId, bankAccountMsg.getId())
                    .eq(BankAccountMsg::getBankAccountId,bankAccountMsg.getBankAccountId())
                    .set(BankAccountMsg::getAccountCertification, dto.getAccountCertification())
                    .set(BankAccountMsg::getCity, dto.getCity())
                    .set(BankAccountMsg::getBankName, dto.getBankName())
                    .set(BankAccountMsg::getType, dto.getType())
                    .set(BankAccountMsg::getBranch, dto.getBranch())
                    .set(BankAccountMsg::getProvince, dto.getProvince())
                    .set(BankAccountMsg::getDistrict, dto.getDistrict())
                    .set(BankAccountMsg::getName, dto.getName())
                    .set(BankAccountMsg::getAccount, dto.getAccount())
                    .set(BankAccountMsg::getType,type)
                    .update();
            BankAccountResDTO bankAccountResDTO = BeanUtil.copyProperties(dto, BankAccountResDTO.class);
            bankAccountResDTO.setUpdateTime(LocalDateTime.now());
            if (!update){
                throw new ForbiddenOperationException("修改失败，请重新尝试");
            }
            bankAccountResDTO.setId(userId);
            bankAccountResDTO.setCreateTime(bankAccountMsg.getCreateTime());
            return bankAccountResDTO;
        }else {
            //新增
            BankAccountResDTO bankAccountResDTO = BeanUtil.copyProperties(dto, BankAccountResDTO.class);
            bankAccountResDTO.setType(type);
            bankAccountResDTO.setId(userId);
            bankAccountResDTO.setCreateTime(LocalDateTime.now());
            bankAccountResDTO.setUpdateTime(LocalDateTime.now());
            BankAccountMsg bankAccountMsg1 = BeanUtil.copyProperties(bankAccountResDTO, BankAccountMsg.class);
            save(bankAccountMsg1);
            return bankAccountResDTO;
        }
    }

    @Override
    public BankAccountResDTO getBankMsgById() {
        Long userId = UserContext.currentUserId();
        BankAccountMsg bankAccountMsg = bankAccountMsgMapper.selectByServerProviderId(userId);
        return BeanUtil.copyProperties(bankAccountMsg, BankAccountResDTO.class);
    }
}
